<template>
  <div class="asset-add">
    <el-card class="form-card" shadow="hover">
      <template #header>
        <div class="card-header">
          <span>添加资产</span>
        </div>
      </template>
      
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="120px"
        class="asset-form"
      >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="资产编码" prop="code">
              <el-input v-model="form.code" placeholder="请输入资产编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资产名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入资产名称" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="资产分类" prop="category">
              <el-select v-model="form.category" placeholder="请选择分类" style="width: 100%">
                <el-option
                  v-for="category in categories"
                  :key="category.value"
                  :label="category.label"
                  :value="category.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资产价值" prop="value">
              <el-input-number
                v-model="form.value"
                :min="0"
                :precision="2"
                style="width: 100%"
                placeholder="请输入资产价值"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择状态" style="width: 100%">
                <el-option label="正常" value="正常" />
                <el-option label="维护中" value="维护中" />
                <el-option label="报废" value="报废" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="位置" prop="location">
              <el-input v-model="form.location" placeholder="请输入资产位置" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="购买日期" prop="purchaseDate">
              <el-date-picker
                v-model="form.purchaseDate"
                type="date"
                placeholder="请选择购买日期"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商" prop="supplier">
              <el-input v-model="form.supplier" placeholder="请输入供应商" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="保修期" prop="warranty">
              <el-input v-model="form.warranty" placeholder="请输入保修期" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资产描述" prop="description">
              <el-input
                v-model="form.description"
                type="textarea"
                :rows="4"
                placeholder="请输入资产描述"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            :rows="3"
            placeholder="请输入备注信息"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleSubmit" :loading="submitting">
            保存
          </el-button>
          <el-button @click="handleCancel">取消</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { assetApi, categoryApi } from '@/api'
import { AssetStatusTextToCode } from '@/utils/status'

const router = useRouter()

// 分类选项
const categories = ref<any[]>([])

const loadCategories = async () => {
  const list: any = await categoryApi.getAll()
  categories.value = (list || []).map((c: any) => ({ label: c.name, value: c.id }))
}

// 表单数据
const form = reactive({
  code: '',
  name: '',
  category: '',
  value: 0,
  status: '正常',
  location: '',
  purchaseDate: '',
  supplier: '',
  warranty: '',
  description: '',
  remark: ''
})

// 表单验证规则
const rules = {
  code: [
    { required: true, message: '请输入资产编码', trigger: 'blur' }
  ],
  name: [
    { required: true, message: '请输入资产名称', trigger: 'blur' }
  ],
  category: [
    { required: true, message: '请选择资产分类', trigger: 'change' }
  ],
  value: [
    { required: true, message: '请输入资产价值', trigger: 'blur' }
  ],
  status: [
    { required: true, message: '请选择状态', trigger: 'change' }
  ]
}

// 加载状态
const submitting = ref(false)
const formRef = ref()

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    submitting.value = true
    
    await assetApi.add({
      ...form,
      categoryId: form.category,
      status: AssetStatusTextToCode[form.status],
      code: form.code,
    })
    ElMessage.success('添加成功')
    router.push('/asset/list')
  } catch (error) {
    submitting.value = false
  }
}

// 取消
const handleCancel = () => {
  router.back()
}

// 重置表单
const handleReset = () => {
  if (formRef.value) {
    formRef.value.resetFields()
  }
  Object.assign(form, {
    code: '',
    name: '',
    category: '',
    value: 0,
    status: '正常',
    location: '',
    purchaseDate: '',
    supplier: '',
    warranty: '',
    description: '',
    remark: ''
  })
}

onMounted(() => {
  loadCategories()
})
</script>

<style scoped>
.asset-add {
  padding: 0;
}

.form-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.asset-form {
  max-width: 800px;
  margin: 0 auto;
}
</style> 